/*
 * @Descripttion :
 * @Author       : QianFox Team
 * @Date         : 2022-03-01 16:53:28
 * @LastEditors  : Please set LastEditors
 * @LastEditTime : 2023-02-28 16:41:48
 */

function Input() {
    // 密码
    this.PASSWORD = '.foxui-suffix-password';
    // 带数量
    this.COUNT = '.foxui-suffix-count';
    this.INPUT = 'input[maxlength]';
}

Input.prototype._init = function () {
    this._bindPasswordToggle();
    this._bindCountInit();
    this._bindCountInput();
};

Input.prototype._bindPasswordToggle = function () {
    $(document).on('click', this.PASSWORD, function () {
        let $this = $(this);
        let $input = $this.siblings('input');
        let isVisible = $(this).is('.foxui-icon-xianshi-o');
        if (isVisible) {
            $input.attr('type', 'text');
            $this.removeClass('foxui-icon-xianshi-o').addClass('foxui-icon-yincang-o');
        } else {
            $input.attr('type', 'password');
            $this.removeClass('foxui-icon-yincang-o').addClass('foxui-icon-xianshi-o');
        }
    });
};

Input.prototype._bindCountInit = function () {
    let that = this;
    // 后追加的不能初始化显示 --> 待解决 bug
    $(document).ready(function () {
        $(that.INPUT).each(function () {
            that._setCount(this);
        });
    });
};

Input.prototype._bindCountInput = function () {
    let that = this;
    $(document).on('input', this.INPUT, function () {
        that._setCount(this);
    });
};

Input.prototype._setCount = function (input) {
    let $input = $(input);
    let len = $input.val().length;
    let maxLen = $input.attr('maxlength');
    $input.siblings(this.COUNT).text(len + '/' + maxLen);
};

let input = new Input();

export { input };
